﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FireBillBook.Core.Entities;
using FireBillBook.Core.Repos.Interfaces;
using Microsoft.Extensions.Logging;
using FireBillBook.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using FireBillBook.Core.Models.Options;
using FireBillBook.Core.Models;

namespace FireBillBook.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BasicSettingController : ControllerBase
    {
        private ILogger _log;
        private IServiceProvider _provider { get; }

        public BasicSettingController(ILogger<BasicSettingController> logger, IServiceProvider provider)
        {
            _log = logger;
            _provider = provider;
        }


        #region Platform
        [HttpGet("Platform/[action]")]
        [ProducesResponseType(typeof(CommonResponseModel<List<TradingPlatform>>), 200)]
        [ProducesErrorResponseType(typeof(BaseReponseModel))]
        public async Task<BaseReponseModel> ListAll([FromServices]IPlatformStore store)
        {
            try
            {
                var platforms = await store.GetAllSync();
                return new CommonResponseModel<List<TradingPlatform>>(platforms)
                {
                    IsSuccess = true,
                };
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "获取所有平台信息异常");
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Message = ex.Message
                };
            }
        }

        [HttpPost("Platform/[action]")]
        public async Task<BaseReponseModel> Save([FromServices]IPlatformStore store, TradingPlatform platform)
        {
            try
            {
                await store.SaveAsync(platform);
                return new BaseReponseModel()
                {
                    IsSuccess = true,
                    Message = "success"
                };
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "保存异常");
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Message = ex.Message
                };
            }
        }

        [HttpPost("Platform/[action]/{name}")]
        public async Task<BaseReponseModel> Delete([FromServices]IPlatformStore store, string name)
        {
            try
            {
                await store.DeleteAsync(name);
                return new BaseReponseModel()
                {
                    IsSuccess = true,
                    Message = "success"
                };
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "保存异常");
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Message = ex.Message
                };
            }
        }

        [HttpPost("Platform/[action]/{name}")]
        public async Task<BaseReponseModel> Detail([FromServices] IPlatformStore store, string name)
        {
            try
            {
                var platform = await store.GetByNameAsync(name);
                return new CommonResponseModel<TradingPlatform>(platform)
                {
                    IsSuccess = true,
                    Message = "success"
                };
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "保存异常");
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Message = ex.Message
                };
            }
        }

        [HttpGet("Platform/Registerd")]
        [ProducesResponseType(typeof(CommonResponseModel<Dictionary<string, string>>), 200)]
        [ProducesErrorResponseType(typeof(BaseReponseModel))]
        public BaseReponseModel GetPlatformServiceInfos()
        {
            try
            {
                var options = _provider.GetRequiredService<IOptionsMonitor<PlatformServiceInfoOptions>>();
                return new CommonResponseModel<List<PlatformServiceInfo>>(options.CurrentValue.Infos.Select(a=>a.Value).ToList())
                {
                    IsSuccess = true,
                    Message = "success"
                };
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "获取已注入服务信息失败");
                return new BaseReponseModel()
                {
                    IsSuccess = false,
                    Message = ex.Message
                };
            }
        }
        #endregion
    }
}
